package com.zzy.service.impl;

import com.zzy.model.ScheduledCron;
import com.zzy.repository.ScheduledCronRepository;
import com.zzy.service.ScheduledCronService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ScheduledCronServiceImpl implements ScheduledCronService {
    @Autowired
    private ScheduledCronRepository scheduledCronRepository;

    @Override
    public List<ScheduledCron> findAll() {
        return scheduledCronRepository.findAll();
    }

    @Override
    public ScheduledCron findByID(Integer id) {
        ScheduledCron model = new ScheduledCron();
        model.setId(id);
        return findByParam(model);
    }

    @Override
    public ScheduledCron findByParam(ScheduledCron model) {
        if(model==null){
            model = new ScheduledCron();//保证不报错
        }
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                //组合查询条件开始 contains  startsWith  endsWith
                .withMatcher("id", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("cronKey", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("cronExpression", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("taskExplain", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnoreCase()  //忽略大小写查询
                .withIgnoreNullValues() //忽略 null 值 对 int 类型 无效 因此 int 使用 Integer
                .withIgnorePaths("state");  //忽略属性
        Example<ScheduledCron> e = Example.of(model, matcher);
        return scheduledCronRepository.findOne(e).get();
    }

    @Override
    public List<ScheduledCron> findListByParam(ScheduledCron model) {
        if(model==null){
            model = new ScheduledCron();//保证不报错
        }
        List<ScheduledCron> result = new ArrayList();
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                //组合查询条件开始 contains  startsWith  endsWith
                .withMatcher("id", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("cronKey", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("cronExpression", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("taskExplain", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnoreCase()  //忽略大小写查询
                .withIgnoreNullValues(); //忽略 null 值 对 int 类型 无效 因此 int 使用 Integer
        //.withIgnorePaths("state");  //忽略属性
        Example<ScheduledCron> e = Example.of(model, matcher);
        result = scheduledCronRepository.findAll(e);
        return  result;
    }

    @Override
    public List<ScheduledCron> findListByPage(ScheduledCron model, Integer pageNO, Integer pageSize) {
        List<ScheduledCron> result = new ArrayList();
        if(model==null){
            model = new ScheduledCron();//保证不报错
        }
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                //组合查询条件开始 contains  startsWith  endsWith
                .withMatcher("id", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("cronKey", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("cronExpression", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("taskExplain", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnoreCase()  //忽略大小写查询
                .withIgnoreNullValues(); //忽略 null 值 对 int 类型 无效 因此 int 使用 Integer
        //.withIgnorePaths("state");  //忽略属性

        //注意调用时
        // 如果 获取不到数据    pageNO从 0 调用即可   好像有缓存一样 暂时不知道为什么
        Sort sort = new Sort(Sort.Direction.DESC, "id");// 排序
        Pageable pageable = new PageRequest(pageNO-1, pageSize, sort);//分页组件
        // 带查询条件
        Example<ScheduledCron> example = Example.of(model, matcher);//加载查询条件
        Page<ScheduledCron> page2 = scheduledCronRepository.findAll(example,pageable);//分页带条件获取值
        result =  page2.getContent();
        return result;
    }


    @CacheEvict(cacheNames="secondlevels",allEntries = true)
    public void save(ScheduledCron model) {
        if(model==null){
            model = new ScheduledCron();//保证不报错
        }
        scheduledCronRepository.saveAndFlush(model);
    }


    @Modifying //定义为 修改
    @CacheEvict(cacheNames="secondlevels",allEntries = true)
    public void update(ScheduledCron model) {
        if(model==null){
            model = new ScheduledCron();//保证不报错
        }
        ScheduledCron modelUpt = findByID(model.getId());//先根据ID 获取到该 实体
        //这样就 实现 修改了
        if(model.getCronKey()!=null){
            modelUpt.setCronKey(model.getCronKey());
        }
        if(model.getCronExpression()!=null){
            modelUpt.setCronExpression(model.getCronExpression());
        }
        if(model.getStatus()!=null){
            modelUpt.setStatus(model.getStatus());
        }
        if(model.getTaskExplain()!=null){
            modelUpt.setTaskExplain(model.getTaskExplain());
        }
    }

    public void delete(Integer id) {
        ScheduledCron model = new ScheduledCron();
        model.setId(id);
        scheduledCronRepository.delete(model);
    }

    public ScheduledCron findByCronKey(String CronKey){
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                //组合查询条件开始 contains  startsWith  endsWith
                .withMatcher("cronKey", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnoreCase()  //忽略大小写查询
                .withIgnoreNullValues(); //忽略 null 值 对 int 类型 无效 因此 int 使用 Integer
        //.withIgnorePaths("state");  //忽略属性
        ScheduledCron model = new ScheduledCron();
        Example<ScheduledCron> e = Example.of(model, matcher);
        return scheduledCronRepository.findOne(e).get() ;
    }
}
